package design.simdi.code.final_paper.site.panels.licente.admisi;

import java.util.ArrayList;
import java.util.List;

import org.apache.wicket.Page;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.model.Model;
import org.apache.wicket.spring.injection.annot.SpringBean;

import design.simdi.code.final_paper.core.entities.Person;
import design.simdi.code.final_paper.core.entities.StudentAdmis;
import design.simdi.code.final_paper.core.services.ApplicationService;
import design.simdi.code.final_paper.core.services.ServiceException;
import design.simdi.code.final_paper.site.application.MyAuthenticatedWebSession;
import design.simdi.code.final_paper.site.panels.blog.TrimiteMesajPanel;
import design.simdi.code.final_paper.utils.ActionToolbar;
import design.simdi.code.final_paper.utils.MyDataTable;
import design.simdi.code.final_paper.utils.ToolbarItem;

/**
 * 
 * @author Marius Dumitriu - (Jun 25, 2010 - 1:28:14 AM)
 * 
 */
public class AdmisiActionToolbar extends ActionToolbar {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	@SpringBean
	private ApplicationService applicationService;

	public AdmisiActionToolbar(MyDataTable<?> table) {
		super(table);

		setOutputMarkupId(true);
	}

	@Override
	public List<ToolbarItem> getItemsList() {
		List<ToolbarItem> lista = new ArrayList<ToolbarItem>();
		if (MyAuthenticatedWebSession.get().isSignedIn())
			lista.add(ToolbarItem.SEND_MESSAGE);
		return lista;
	}

	@Override
	public void onAction(ToolbarItem toolbarItem, AjaxRequestTarget target) {

		switch (toolbarItem) {
		case SEND_MESSAGE:
			onSendMessage(target);
			break;
		}

	}

	private void onSendMessage(AjaxRequestTarget target) {
		try {
			if (getTable().getSelectedRow() == null)
				throw new ServiceException("table.row.not.selected");

			StudentAdmis sc = (StudentAdmis) getTable().getSelectedRow().getModelObject();
			Person p = getApplicationService().findPersoanaByStudentAdmis(sc);

			TrimiteMesajPanel sendPanel = new TrimiteMesajPanel("panel", new Model<Person>(p), this.getPage().get(
					"content:tabs:panel"));
			sendPanel.setOutputMarkupId(true);
			Page page = this.getPage();

			page.get("content:tabs:panel").replaceWith(sendPanel);

			target.addComponent(page.get("content"));

		} catch (ServiceException e) {
			warn(getString(e.getMessage()));

		}

	}

	@Override
	public String getToolbarName(ToolbarItem toolbarItem) {
		return getString(toolbarItem.getName());
	}

	public void setApplicationService(ApplicationService applicationService) {
		this.applicationService = applicationService;
	}

	public ApplicationService getApplicationService() {
		return applicationService;
	}

}
